package com.example.lenovo.manager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.List;

public class LoginPage extends AppCompatActivity {

    private EditText userName,passWord;
    private Button loginButton,registerButton;
    private TextView ErrorOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        AVOSCloud.initialize(this, "Po6OudEc9QxRgv1qcussxBxi-gzGzoHsz", "EFNsW7y5aXtKMkpU50xyhHNk");

        userName = (EditText) findViewById(R.id.UserName);
        passWord = (EditText) findViewById(R.id.PassWord);
        loginButton = (Button) findViewById(R.id.Login);
        registerButton = (Button) findViewById(R.id.Register);
        ErrorOut = (TextView) findViewById(R.id.ErrorOut);

        /*点击登录按钮，做登录验证*/
        loginButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ErrorOut.setText("");
                        if(LoginCheck()){
                            LoginOperator();
                        }
                    }
                }
        );

        /*点击注册按钮，跳转注册页面*/
        registerButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(LoginPage.this,RegisterPage.class);
                        startActivity(intent);
                    }
                }
        );


    }

    /**
     * 登录用户名密码非空验证
     * @return
     */
    public boolean LoginCheck(){
        if(userName.getText().toString().isEmpty()){
            ErrorOut.setText("*用户名不能为空！");
            return false;
        }
        if(passWord.getText().toString().isEmpty()){
            ErrorOut.setText("*密码不能为空");
            return false;
        }
        return true;
    }

    /**
     * 登录函数
     * @return
     */
    public void LoginOperator(){

        AVQuery<AVObject> query = new AVQuery<>("ManagerUser");
        query.whereEqualTo("UserName", userName.getText().toString());
        query.whereEqualTo("PassWord", passWord.getText().toString());
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(list.isEmpty()){
                    ErrorOut.setText("*用户名密码有误，请重新登陆");
                }
                else {
                    if(list.get(0).getInt("Sure") == 1) {
                        Intent intent = new Intent();
                        intent.setClass(LoginPage.this, IndexPage.class);
                        startActivity(intent);
                    }
                    else{
                        dialog1();
                    }
                }
            }
        });

    }


    /**
     * 注册成功对话框
     */
    private void dialog1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("登录信息");
        builder.setMessage("您的账户尚未认证，无法登陆！");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); //关闭dialog
            }
        });

        builder.create().show();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login_page, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
